package cn.czg.cms.controller;

import cn.czg.cms.common.ResultT;
import cn.czg.cms.entity.dto.PostDTO;
import cn.czg.cms.entity.dto.PostWithUser;
import cn.czg.cms.entity.req.PostCreateRequest;
import cn.czg.cms.service.PostService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/cms/post")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("/page")
    public ResultT<IPage<PostDTO>> list(@RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "10") int size,
                                        @RequestParam(required = false) String sort,
                                        String type,
                                        @RequestParam(required = false) Double lat,
                                        @RequestParam(required = false) Double lng, String searchKey) {

        IPage<PostDTO> pageData = postService.pageList(lng, lat, sort, type, current, size, searchKey);

        return ResultT.success(pageData);
    }

    @PostMapping("/like")
    public ResultT<Boolean> post(String postId, int like) {
        postService.updateLikeCount(postId, like);
        return ResultT.success();
    }


    @PostMapping("/publish")
    public ResultT<Boolean> like(@RequestBody PostCreateRequest postCreateRequest) {
        postService.createPost(postCreateRequest);
        return ResultT.success();
    }

    @GetMapping("/listByUser")
    public ResultT<IPage<PostWithUser>> listByUser(@RequestParam Long userId,
                                                   @RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "10") int pageSize) {
        IPage<PostWithUser> page = postService.listByUser(userId, pageNum, pageSize);
        return ResultT.success(page);
    }


    @GetMapping("/detail")
    public ResultT<PostWithUser> detail(String id) {
        return ResultT.success(postService.detail(id));
    }

}
